﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;

public partial class UserControl_ControlChuyenDoanhSoNPP : System.Web.UI.UserControl
{
    ClassExecute.ClassExecute dt;
    string str = webapp4U.Configuration.CurrentConnectionString;
    string error = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (ViewState["flag"] == null)
            {
                ViewState["flag"] = false;
            }

            LoadChiNhanh();
        }
    }
    protected void imgBtnAdd_Click(object sender, ImageClickEventArgs e)
    {
        Reset();
    }
    protected void ImageBtnCheck_Click(object sender, ImageClickEventArgs e)
    {
        dt = new ClassExecute.ClassExecute();
        try
        {

            int iCheckMonth = 1;
            iCheckMonth = dt.ExecuteSQl_int(str, "[ls_isClosedMonth]", CommandType.StoredProcedure, ref error,
                new SqlParameter("@selDate", txtOrderDate.Text.Trim()),
                new SqlParameter("@siteCode", ddlChiNhanh.SelectedValue.ToString()));
            if (iCheckMonth == 0)
            {
                int iCheckHD = dt.ExecuteSQl_int(str, "[ls_isValidInvoce4Change]", CommandType.StoredProcedure, ref error,
                        new SqlParameter("@siteCode", ddlChiNhanh.SelectedValue.ToString()),
                        new SqlParameter("@invDate", txtOrderDate.Text.Trim()),
                        new SqlParameter("@salespKey", txtMaNPP.Text.Trim()),
                        new SqlParameter("@invNo", txtOrderDate.Text.Trim()));

                if (iCheckHD == 0)
                {
                    txtMaNPP.Text = "";
                    txtNameNPP.Text = "";
                    txtSoHD.Text = "";
                    txtTienHD.Text = "";
                    uscMsgBox1.AddMessage("Không có đơn hàng số " + txtSoHD.Text + " của NPP " + txtMaNPP.Text + " thuộc TTPP " + ddlChiNhanh.SelectedItem.ToString() + " phát sinh ngày " + txtOrderDate.Text, uscMsgBox.enmMessageType.Error);

                }
                else if (iCheckHD == 1)
                {
                   
                    txtTienHD.Text = dt.GetDataDepend(str, "[ls_getInvoiceAmount]", CommandType.StoredProcedure, ref error, new SqlParameter("@invDate", txtOrderDate.Text.Trim()), new SqlParameter("@invNo", txtSoHD.Text));
                    LockText();
                   ViewState["flag"] = true;
                }

            }
            else
            {

                uscMsgBox1.AddMessage("Ngày " + txtOrderDate.Text + " đã thuộc tháng khóa sổ của TTPP " + ddlChiNhanh.SelectedItem.ToString(), uscMsgBox.enmMessageType.Error);
            }
        }
        catch (Exception ex)
        {

            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
        }
     }      
    protected void imgBtnSave_Click(object sender, ImageClickEventArgs e)
    {
        dt = new ClassExecute.ClassExecute();
        try
        {
           
            if ((bool)ViewState["flag"])
            {
                bool kq = dt.ExecuteSQl_bool(str, CommandType.StoredProcedure, "[ls_changeBA4Invoice]", ref error,
                    new SqlParameter("@invNo", txtSoHD.Text.Trim()),
                    new SqlParameter("@invDate", txtOrderDate.Text.Trim()),
                    new SqlParameter("@toSalesPKey", txtMaNPPMoi.Text.Trim()));
                if (kq)
                {
                    uscMsgBox1.AddMessage("Chuyển doanh số thành công", uscMsgBox.enmMessageType.Success);
                    Reset();

                }
                else
                {
                    uscMsgBox1.AddMessage("Chuyển doanh số thất bại", uscMsgBox.enmMessageType.Error);
                }
                ViewState["flag"] = false;
            }
            else
            {
                uscMsgBox1.AddMessage("Bạn chưa thực hiện nghiệp vụ kiểm tra", uscMsgBox.enmMessageType.Error);
            }
        }
        catch (Exception ex)
        {

            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
        }
    }
    void LoadChiNhanh()
    {
        dt = new ClassExecute.ClassExecute();
        try
        {

            DataTable db = dt.Get_Data_DataTable(str, ref error, "[ls_getDistributorList]", CommandType.StoredProcedure);
            dt.BindDropDownList(db, ddlChiNhanh, "Sitecode", "sitename");
        }
        catch (Exception ex)
        {

            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
        }
    }
    protected void txtMaNPP_TextChanged(object sender, EventArgs e)
    {
        dt = new ClassExecute.ClassExecute();
        try
        {
            txtNameNPPMoi.Text = dt.GetDataDepend(str, "k_getSalesp_nameBySitecode", CommandType.StoredProcedure, ref error,
                new SqlParameter("@salesp_key", txtMaNPPMoi.Text),
                new SqlParameter("@Sitecode", ddlChiNhanh.SelectedValue.ToString()));

        }
        catch (Exception ex)
        {

            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
        }
    }
    protected void txtMaNPPMoi_TextChanged(object sender, EventArgs e)
    {
        dt = new ClassExecute.ClassExecute();
        try
        {
            txtNameNPPMoi.Text = dt.GetDataDepend(str, "k_getSalesp_nameBySitecode", CommandType.StoredProcedure, ref error,
                new SqlParameter("@salesp_key", txtMaNPPMoi.Text),
                new SqlParameter("@Sitecode", ddlChiNhanh.SelectedValue.ToString()));
            
        }
        catch (Exception ex)
        {

            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
        }
    }
    void LockText()
    {
        
        ddlChiNhanh.BackColor = Color.Yellow;
        txtOrderDate.BackColor = Color.Yellow;
        txtMaNPP.BackColor = Color.Yellow;
        txtSoHD.BackColor = Color.Yellow;
        txtMaNPPMoi.BackColor = Color.Yellow;

        ddlChiNhanh.Enabled = false;
        txtOrderDate.Enabled = false;
        txtMaNPP.ReadOnly = true;
        txtSoHD.ReadOnly = true;
        txtMaNPPMoi.ReadOnly = true;
    }
    void Reset()
    {
        
        txtMaNPP.Text = "";
        txtNameNPP.Text = "";
        txtMaNPPMoi.Text = "";
        txtNameNPPMoi.Text = "";
        txtSoHD.Text = "";
        txtTienHD.Text = "";
        ddlChiNhanh.SelectedIndex = 0;
        txtOrderDate.Text = DateTime.Now.ToString("dd/MM/yyyy");

        ddlChiNhanh.BackColor = Color.White;
        txtOrderDate.BackColor = Color.White;
        txtMaNPP.BackColor = Color.White;
        txtSoHD.BackColor = Color.White;
        txtMaNPPMoi.BackColor = Color.White;

        ddlChiNhanh.Enabled = true;
        txtOrderDate.Enabled = true;
        txtMaNPP.ReadOnly = false;
        txtSoHD.ReadOnly = false;
        txtMaNPPMoi.ReadOnly = false;
    }

}